package com.hongyi.controller;

/**
 * @Author: hongyi
 * @CreateTime: 2024-10-21
 */


import com.hongyi.dto.PostDto;
import com.hongyi.pojo.Post;
import com.hongyi.result.Result;
import com.hongyi.service.PostService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/posts")
public class PostController {
    private final PostService postService;

    public PostController(PostService postService) {
        this.postService = postService;
    }

    @GetMapping("/{id}")
    public Result<PostDto> getPost(@PathVariable int id) {
        PostDto postDto;
        try {
            postDto = postService.getPostById(id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success(postDto);
    }
}
